package com.cyz.web.app.concurrent;

import lombok.AllArgsConstructor;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@AllArgsConstructor
public class DeadLockDemo {

    public static final String A = "A";

    public static final String B = "B";

    private static final Lock lock= new ReentrantLock();

    public static void main(String[] args) {
        DeadLockDemo demo = new DeadLockDemo();
        lock.tryLock();
        demo.deadLock();
    }

    private void deadLock(){
         Thread t1 = new Thread(() -> {
             synchronized(A){
                 try {
                     Thread.sleep(2000);
                 }catch(InterruptedException e){
                     e.printStackTrace();
                 }

                 synchronized(B){
                     System.out.println("1");
                 }
             }
         });

         Thread t2 = new Thread(() ->{
                synchronized(B){
                    synchronized(A){
                        System.out.println("2");
                    }
                }
         });

         t1.start();
         t2.start();
    }



}
